var tree;
var ds, grid;

var buildTree = function() {
    var root = new Ext.tree.AsyncTreeNode({
        id:'0',
        xtype:'fl',
        type:'',
        text:'疾病分类查询'
    });
    tree = new Ext.tree.TreePanel({
        loader:new Ext.tree.TreeLoader({dataUrl:'dict!getTrees.action'}),
        root:root,
        renderTo:'tree',
        border:false,
        autoScroll:true
    });
    
    tree.on('click',function(node){
		
        ds.setBaseParam('flid',node.id);
		ds.setBaseParam('lb',node.attributes.xtype);
        ds.load({params:{parent_id:node.id}});
        node.expand();
        node.select();
    });
    tree.on('beforeload',function(node){
        tree.loader.dataUrl = 'icd!getTree.action?parent_id=' + node.id+'&type='+node.attributes.type+'&xtype='+node.attributes.xtype;
    });
	
};
var buildGrid = function() {
    ds = new Ext.data.JsonStore({
        url:'icd!getChildren.action',
        root:'items',
        totalProperty:'total',
        idProperty:'icd.id',
        fields:['icd.id','icd.bm','icd.mc','icd.zjm','icd.lb','icd.flid']
    });

	grid = new Ext.grid.EditorGridPanel({
		store : ds,
		columns : [new Ext.grid.RowNumberer(), {
			header : '编码',
			width : 150,
			sortable : true,
			dataIndex : 'icd.bm'
		}, {
			header : '名称',
			width : 200,
			sortable : true,
			dataIndex : 'icd.mc'
		}, {
			header : '助记码',
			width : 150,
			sortable : true,
			dataIndex : 'icd.zjm'
		}],
		border : false,
		selModel : new Ext.grid.RowSelectionModel(),
		bbar : new Ext.PagingToolbar({
							store : ds,
							pageSize : 30,
							displayInfo : true,
							beforePageText : '页',
							afterPageText : '共 {0} 页',
							displayMsg : '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
							emptyMsg : "没有记录",
							firstText : '第一页',
							prevText : '前一页',
							nextText : '下一页',
							lastText : '最后一页',
							refreshText : '刷新'
						}),
		loadMask : {
			msg : '数据加载中...'
		}
	});
	ds.load();
};

var buildLayout = function(){
    new Ext.Viewport({
        layout:'border',
        defaults:{border:false},
        items:[{
            region:'north',
            contentEl:'north-div',
            title:'疾病编码查询'
        },{
            region:'west',
            width:380,
            split:true,
            autoScroll:true,
            items:tree
        },{
            region:'center',
            layout:'fit',
            items:grid
        }]
    });
};

var init = function() {
	buildTree();
	buildGrid();
	buildLayout();
};

Ext.onReady(init);